The Last Giants

A data-driven safari into the savannah

Author

Visual Virtuosos

Abstract

African elephants face severe threats from poaching and the loss of their natural habitats. Despite global attention towards their conservation, there is considerable uncertainty surrounding the population sizes and trends of elephants across much of Africa. Timely and accurate data on elephant populations is crucial for effective conservation efforts.

A continent-wide survey conducted by the Chase, M.J. et al., 2016` revealed a stark decline in African Savannah Elephants. Africa may have held over 20 million elephants before European colonization and 1 million as recently as the 1970s. The survey estimated a population of 352,271 savannah elephants across study sites in 18 countries, representing approximately 93% of all savannah elephants in those areas. Alarmingly, regions with historical data indicated a significant decline, estimating a decrease of 144,000 elephants between 2007 and 2014.

This project aims to examine how elephant populations across Africa changed between 1995 and 2014. It involves using graphs and maps to show how the number of elephants varied over these years. To understand what might happen in the future, the project will use OLS regression mathematical method to guess how many elephants there might be from 2015 to 2023.

Moreover, this study will also look into whether there’s a connection between how many elephants there are and the trade of ivory. By comparing data about how many elephants there are with information about ivory trade, the project will try to find out if ivory trade affects the number of elephants.

The main goals are to see how elephant numbers changed over time, predict how many might exist in the coming years, and understand if the ivory trade has any impact on elephant populations. This research aims to help conservationists better understand and protect these majestic animals.

Introduction

The African elephant(Loxodonta africana) aka The Gentle Giants, a keystone species crucial to the continent’s ecosystem, has faced significant population challenges over the decades due to various factors such as poaching, habitat loss, and human-wildlife conflicts. Understanding the historical trends in elephant populations is fundamental in formulating effective conservation strategies and policies. This project aims to comprehensively analyze the population dynamics of African elephants spanning from 1950 to 2015 by leveraging robust data visualization techniques and advanced statistical methods.

Using datasets sourced from reputable conservation organizations and wildlife databases, the project begins by visually mapping the fluctuations in elephant populations across different regions of Africa over the studied period. Subsequently, employing multiple regression methods, including but not limited to linear regression and time series analysis, the study intends to forecast the elephant population from 2016 to 2023 based on historical data trends.

By combining data visualization techniques, statistical analyses, and exploration of the complex interplay between elephant populations and the ivory trade, this project endeavors to contribute valuable insights into the dynamics of African elephant conservation and the factors influencing their populations. Ultimately, the findings may aid policymakers, conservationists, and wildlife experts in formulating more targeted and effective measures to safeguard these majestic creatures and their habitats.

3D Visualization

The following animation (a rendered 3D-plot in .mp4) highlights those African countries which have the Savannah elephant ecosystems.
The specific countries are projected in 3-dimensional on the basis of the area of the ecosystems.

Code
#------------------- Installing libraries-------------------------------------

library(circularEV)
library(rnaturalearth)
library(ggplot2)
library(plotly)
library(ggiraph)
library(ggshadow)

library(raster)
library(rayshader)
library(rgl)

#------------------- Mapping the dataset with the "Africa" dataset in rnaturalearth-------

africa <- ne_countries(scale = "medium", returnclass = "sf", continent = "Africa")
cnt <- ne_countries(scale = "medium", returnclass = "sf", country = "sudan")
cnt2 <- ne_countries(scale = "medium", returnclass = "sf", country = "senegal")
col1 <- c("Angola", "Botswana", "Cameroon", "Chad", "Congo", "Kenya","Malawi",
          "Mali", "Mozambique", "South Africa", "Tanzania","Uganda", "Zambia", "Zimbabwe")
#col2 <- c(20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20)

col3 <- c(43459, 101599, 20598, 9799, 9349, 85711, 2991, 3944, 101077, 19809, 268250,
          10938, 84814, 68851)

eco <- data.frame(col1,col3)

eco2 <- merge(africa, eco, by.x = "name", by.y = "col1")


#------------------- Creating the ggplot -------------------------------------
map <- ggplot() +
  geom_sf(data = africa) +
  geom_sf(data= eco2, aes(fill = col3), color = "white", linewidth = 0.5)+
  guides(fill=guide_legend(title=bquote('Area'~(km^2)), reverse=TRUE)
         )+
  theme_void()+
  theme(
    plot.background = element_rect(
      fill = "white",
      colour = "white"
    ))  
map

#------------------- Converting the 2-D ggplot to 3-D-------------------------------------

map3d <- plot_gg(map,
                 width=5,
                 height=5,
                 scale = 350,
                 windowsize = c(1000, 1000),
                 phi = 30,
                 save_height_matrix = FALSE,
                 theta = 0,
                 flat_transparent_bg = TRUE,
                 invert = FALSE
                 )

#------------------- Annotating the top countries-------------------------------------

Tanzania_coords <- c(x = 200, y = 520, z = 50)
rgl::text3d(x = Tanzania_coords[1], y = Tanzania_coords[2], z = Tanzania_coords[3],
            text = "Tanzania", col = "black", cex = 0.9)

rgl::lines3d(c(200, 200, 200),
             c(50, 100, 500),
             c(50, 50, 50),
             col = "black")



Botswana_coords <- c(x = 50, y = 420, z = 250)
rgl::text3d(x = Botswana_coords[1], y = Botswana_coords[2], z = Botswana_coords[3],
            text = "Botswana", col = "black", cex = 0.9)

rgl::lines3d(c(50, 50, 50),
             c(50, 100, 400),
             c(250, 250, 250),
             col = "black")


Mozam_coords <- c(x = 220, y = 320, z = 190)
rgl::text3d(x = Mozam_coords[1], y = Mozam_coords[2], z = Mozam_coords[3],
            text = "Mozambique", col = "black", cex = 0.9)

rgl::lines3d(c(220, 220, 220),
             c(50, 100, 300),
             c(190, 190, 190),
             col = "black")


kenya_coords <- c(x = 250, y = 270, z = -90)
rgl::text3d(x = kenya_coords[1], y = kenya_coords[2], z = kenya_coords[3],
            text = "Kenya", col = "black", cex = 0.9)

rgl::lines3d(c(250, 250, 250),
             c(50, 100, 250),
             c(-90, -90, -90),
             col = "black")




zambia_coords <- c(x = 50, y = 220, z = 150)
rgl::text3d(x = zambia_coords[1], y = zambia_coords[2], z = zambia_coords[3],
            text = "Zambia", col = "black", cex = 0.9)

rgl::lines3d(c(50, 50, 50),
             c(50, 100, 200),
             c(150, 150, 150),
             col = "black")


# Reference lines

# rgl::lines3d(c(50, 50, 50),
#              c(50, 100, 500),
#              c(50, 50, 50),
#              col = "red")
# 
# rgl::lines3d(c(100, 100, 100),
#              c(50, 100, 500),
#              c(50, 50, 50),
#              col = "darkgreen")


# render_movie(filename = "final3d.mp4", type = "orbit",
#              frames = 360,  phi = 35, zoom = 0.8, theta = -90)


rgl::rglwidget()

Question 1:- How has elephant population varied over the years across several African countries?

Introduction

Elephants are the gentle giants and here we want to know about the trends in their population and to find out whether its declining or is at safe level, and if there is a decrease in their population then to find out the reason the cause. Understanding the population dynamics of African elephants is pivotal for conservation efforts and ecological balance. This will help us in safeguarding their future and to show to next many generations the gentle giants of this planet and how marvelous they are.

The dataset under examination spans from 1995 to 2014, presenting a valuable opportunity to predict and comprehend the trends in elephant populations for the subsequent years i.e. 2015 to 2023. This study aims to investigate the variations in elephants population across several African countries during the period, leveraging a multi-faceted analytical approach including data pre-processing, explanatory analysis, predictive modelling and country-level analysis

Approach

  • We are going to develop code which serves as a robust approach for visualizing and analyzing the fluctuation in elephant population estimates across multiple years within several African countries. Using libraries such as ggplot2 and plotly, this methodology aggregates and processes elephant population data, offering a comprehensive depiction of population trends.

  • We are going to initially read and preprocess the data-set, grouping data by year and summing up estimates, subsequently training an Ordinary Least Squares (OLS) regression model to predict population estimates for future years.

  • Through visualization, we will make interactive plots, showcasing the historical trends of elephant populations from 1995 to 2014, while also projecting estimates for subsequent years i.e. from 2015 to 2023. The plots illustrate the trajectory of elephant population estimates, distinguishing between actual data and predicted values.

  • By merging actual and forecasted population data, the code encapsulates a holistic view, enabling the viewer to comprehend the variations and potential trajectories of elephant populations across different African countries over time.

Analysis

Code
elephant <- readxl::read_xlsx(here("data","elephant_data.xlsx"))
Code
total_estimate_per_year <- elephant |>
  group_by(Year) |>
  summarise(Population = sum(Estimate))

total_estimate_per_year <- total_estimate_per_year |>
  filter(Year != 2015)

# Fitting a linear regression model
lm_model <- lm(Estimate ~ Year, data = elephant)

# Defining a recipe
ols_recipe <- recipe(Population ~ Year, data = total_estimate_per_year) |>
  step_center(Year, all_predictors()) |>
  step_scale(Year, all_predictors())

# Defining the linear regression model
ols_model <- linear_reg() |>
  set_engine("lm")

# Combining recipe and model into a workflow
ols_workflow <- workflow() |>
  add_recipe(ols_recipe) |>
  add_model(ols_model)

# Training the OLS model
ols_fit <- ols_workflow |>
  fit(data = total_estimate_per_year)

# Creating a new data frame for prediction (between years 2015-2023)
new_years <- data.frame(Year = 2015:2023)

# Making predictions using the trained OLS model
ols_predictions <- predict(ols_fit, new_data = new_years)

# Combining the new years and predictions
ols_predictions_data <- data.frame(Year = new_years$Year, Population = ols_predictions$.pred)

# Specify the training control
train_control <- trainControl(method = "cv", number = 5)

# Train the OLS model using cross-validation
ols_model <- train(Population ~ Year, data = total_estimate_per_year, method = "lm", trControl = train_control)


# Plotting the estimated of elephant population between 1995 to 2014
p <- ggplot(total_estimate_per_year, aes(x = Year, y = Population, group = 1)) +
  geom_line() +
  stat_smooth(method = "loess", se = FALSE) +
  ggtitle("Combined Data: Estimate Over Time") +
  xlab("Year") +
  ylab("Estimate") +
  scale_y_continuous(limits = c(0, 500000), expand = c(0, 0), labels = comma_format(scale = 1e-3)) +
  scale_color_manual(values = c("black", "red3"), guide = FALSE) +
  labs(
    x = "Year",
    y = "Population (in Thousands)",
    title = "Trend in Elephant Population from 1995-2014",
    caption = "Source: PeerJ PeerJ Article
doi: 10.7717/peerj.2354 "
  ) +
  theme_minimal()

# Convert ggplot to plotly
population_trend <- ggplotly(p)

population_trend

The population of the elephants from 2015-2023 was predicted using machine learning techniques and eesults of the same are as follows

Model RMSE
OLS 312.46
Poly Regression 326.09
Random Forest 333.23
Code
#| title: ""Visualising the trend in elephant population from 1995-2023""

# Combining both the actual and predicted data using rowbind
combined_data <- rbind(total_estimate_per_year, ols_predictions_data)

# Plotting all the estimates of elephant population between 1995-2023
combined_prediction <- ggplot(combined_data, aes(x = Year, y = Population, group = 1)) +
  geom_line(aes(color = ifelse(Year <= 2014, "Population Trend", "Prediction"))) +
  stat_smooth(method = "loess", se = FALSE) +
  scale_y_continuous(limits = c(0, 500000), expand = c(0, 0), labels = scales::comma_format(scale = 1e-3)) +
  scale_color_manual(values = c("Population Trend" = "black", "Prediction" = "red3"), guide = FALSE) +
  labs(
    x = "Year",
    y = "Population (in Thousands)",
    title = "Trend in Elephant Population from 1995-2023",
    caption = "Source: PeerJ PeerJ Article doi: 10.7717/peerj.2354 ",
  ) +
  theme_minimal()

# Convert ggplot to plotly
combined_prediction_plot <- ggplotly(combined_prediction, tooltip = c("Year", "y"))

combined_prediction_plot

Discussion

  • The line plot depicting the population estimates from 1995 to 2014 reveals annual variations in savannah elephant trends. Utilizing the loess method with geom_smooth, a continuous trend highlights a gradual increase from 1995 until approximately 2003. However, a notable decline follows from 2004, attributed to increased poaching, persisting until 2008. Subsequently, concerted conservation efforts result in a positive trend, aligning with findings in the research paper, indicating the effectiveness of our plot optimization.

  • Conversely, the line plot illustrating predicted population estimates from the OLS regression method demonstrates a consistent, albeit slight, growth in African elephant populations. This prediction strongly relies on previous year estimates, influencing the gradual upward trend observed.

  • The combined plot merges estimate data from 1995 to 2014 with predicted data from 2015 to 2023, displaying an overall trend. The population of elephants exhibits a slow, continuous growth, reflecting conservation efforts aimed at their protection. This cumulative trend is visibly depicted by the geom_smooth line, signifying sustained progress in safeguarding elephant populations.

Question 2:- What is the trend of ivory trade in Africa?

Introduction

The illegal trade of ivory has remained a critical concern, posing significant threats to the African elephant population and biodiversity conservation. This analysis aims to study ivory trade in Africa using available historical data. The dataset covers past ivory trade information and seeks to predict trends from 2020 to 2023. The main goal is to reveal how ivory trade has changed over time by using careful data processing, exploring data, making predictions and creating clear visualizations.

Approach

  • We are going to conducts an analysis of ivory trade in Africa based on data from CITES. We read and process the data-set to count the number of ivory exports per year from 1990 to 2022. It then visualizes this information using a bar plot, showcasing the trend of ivory exports over time. Additionally, it explores the types of ivory exports, displaying their counts through a bar plot. These visualizations enable the observation of trends in ivory trade, showcasing changes in export volumes over the years and providing insights into the types of ivory being traded.

  • The trend of ivory trade in Africa, as depicted by the analysis, showcases the fluctuation in export counts over the years from 1990 to 2022. It allows for a visual understanding of the general trajectory of ivory exports, indicating whether they have increased, decreased, or remained relatively stable over this period. Additionally, the analysis dives into the types of ivory exports, offering insights into the diversity or dominance of specific types in the trade, which contributes to understanding the overall trend of ivory trade in Africa.

Analysis

Visualising the trend in Ivory trade from 1990 to 2022

Code
# Loading ivory data
ivory_exports_data <- read_csv(here("data","cites_ivory_export.csv"))


# Counting the number of instances for each year
export_count_per_year <- ivory_exports_data |>
  group_by(Year) |>
  summarise(export_count = n())

# Assuming `count_per_year` is your dataset
export_count_per_year <- export_count_per_year |>
  filter(Year > 1994)

#glimpse(export_count_per_year)


# Plotting the Ivory exports between 1990 to 2022
exports_barplot <- ggplot(export_count_per_year, aes(x = as.factor(Year), y = export_count, fill = as.factor(Year), text = paste("Export count: ", export_count))) +
  geom_bar(stat = "identity", color = "black") +
  scale_fill_viridis_d(option = "C") +
  labs(
    x = "Year",
    y = "Number of exports",
    title = "Ivory exports in Africa from 1995 to 2022",
    caption = "Source: CITES (Convention of International Trade in Endangered Species of Wild Fauna and Flora)"
  ) +
  guides(fill = FALSE) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Convert ggplot to plotly
exports_plotly <- ggplotly(exports_barplot, tooltip = "text")

# Display the interactive plot
exports_plotly

Visualizing the Ivory exports in Africa among the Top 5 largest savannah elephant ecosystems

Code
#| fig-alt: "Ivory exports from 1995 to 2022 among the Top 5 largest ecosystems"

# Extracing data from ivory_exports dataset
country_export_count <- ivory_exports_data %>%
  filter(Exporter %in% c("ZW", "ZM", "ZA", "BW", "MZ")) %>%
  group_by(Exporter) %>%
  summarise(count = n()) %>%
  mutate(Country = case_when(
    Exporter == "ZW" ~ "Zimbabwe",
    Exporter == "ZM" ~ "Zambia",
    Exporter == "ZA" ~ "South Africa",
    Exporter == "BW" ~ "Botswana",
    Exporter == "MZ" ~ "Mozambique",
    TRUE ~ as.character(Exporter)
  ))

africa <- ne_countries(scale = "medium", continent = "Africa", returnclass = "sf")

export_country_map <- merge(africa, country_export_count, by.x = "name", by.y = "Country")

# Create a ggplot map
gg_export_countries <- ggplot() +
  geom_sf(data = africa, fill = "white") +
  geom_sf(data = export_country_map, aes(fill = count, text = paste("Country: ", country_export_count$Country,"<br>",  
    "Export count: ",count )), color = "#5D2606", size = 0.2) +
  scale_fill_gradient(low = "#F2A64F", high = "#B61210", name = "Export count") + 
  theme_void() +
  labs(title = "Ivory exports from 1995 to 2022 (Top 5 largest ecosystems)", caption = "Source: CITES (Convention of International Trade 
    in Endangered Species of Wild Fauna and Flora)")

# Convert ggplot to plotly
gg_export_countries_plotly <- ggplotly(gg_export_countries, tooltip = "text")

# Show the Plotly map
gg_export_countries_plotly

Visualizing the Type of Ivory exports in Africa

Code
#| fig-alt: Visualizing the Type of Ivory exports in Africa

ivory_term_count <- ivory_exports_data |>
  group_by(Year,Term) |>
  summarise(instance_count = n()) |>
  ungroup()

ivory_term_count <- ivory_term_count |>
  mutate(Term = fct_reorder(Term, instance_count, .desc = TRUE))

carving_ggplot <- ivory_term_count |>
  ggplot(aes( y = fct_rev(Term), size = instance_count, fill = Term)) +
  geom_bar(alpha = 0.7, color = "black") +
  scale_size(range = c(1.4, 10), name = "Population (M)") +
  scale_fill_carto_d(palette = "Safe") +
  theme_minimal() +
  theme(legend.position = "none") +
  labs(
    x = "Count",
    y = "Export Type",
    title = "Type of ivory export",
    caption = "Source: CITES (Convention of International Trade 
    in Endangered Species of Wild Fauna and Flora)"
  )

# converting ggplot to plotly
carvings_plotly <- ggplotly(carving_ggplot, tooltip = "text")

# display the interactive plot
carvings_plotly

Discussion

  • It is to observe there was huge number of exports during the early 2000s which now shows a declining trend.

  • With reference to the line plots from question 1 and the declining trend in the ivory exports, it is to co-relate and interpret that the population of the elephants might gradually increase in near future. The predicted values of the population from 2015-2023 also convey the same. However, the increase in the elephant population also depends on various other factors.